package com.example.multimedia.video

import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.multimedia.R
import com.example.multimedia.databinding.ActivityDABinding

class DAActivity : AppCompatActivity() {
    private lateinit var binding: ActivityDABinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityDABinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val uri = "android.resource://$packageName/${R.raw.video}".toUri()
        binding.videoView.setVideoURI(uri)

        binding.play.setOnClickListener {
            if (!binding.videoView.isPlaying) {
                binding.videoView.start()
            }
        }
        binding.pause.setOnClickListener {
            if (binding.videoView.isPlaying) {
                binding.videoView.pause()
            }
        }
        binding.replay.setOnClickListener {
            if (binding.videoView.isPlaying) {
                binding.videoView.resume()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.videoView.suspend()
    }
}